#include "stack.h"

#define MAXLENGTH 50

Stack StackInit(int initSize) {
    Stack res;
    res = (Stack)malloc(sizeof(Stack));
    res->p = (int*)calloc(initSize + 1, sizeof(int));
    res->size = 0;
    res->top = 0;
    res->capacity = initSize;
    return res;
}

void StackPush(Stack stack, int element) {
    if (stack->size < stack->capacity) {
        stack->p[++stack->top] = element;
    } else {
        ShowInfo("栈已满！");
    }
    stack->size++;
}

int StackTop(Stack stack) {
    if (stack->size > 0) {
        return stack->p[stack->top];
    }
    else {
        ShowInfo("栈为空！");
        return 0;
    }
}

void StackPop(Stack stack) {
    if (stack->size > 0) {
        stack->top--;
        stack->size--;
    } else {
        ShowInfo("栈为空！");
    }
}

void ShowInfo(const char* message) {
    char buff[MAXLENGTH+1];
    snprintf(buff, MAXLENGTH - 11, "%s\n", message);
    fputs(buff, stdout);
}